<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size: 20px;">用户诊断</span>
    <el-button type="primary" style="float:right;margin-top:13px;" @click="dialogVisible=true">添加用户</el-button>
  </div>

  <!-- 显示用户信息表格 -->
  <el-card style="margin:20px;">
    <el-table :data="UserArr">
      <el-table-column type="index" label="编号" width="80" align="center"></el-table-column>
      <el-table-column prop="nickname" label="用户姓名" align="center"></el-table-column>
      <el-table-column prop="gender" label="用户性别" align="center" :formatter="UserGenderFormatter"></el-table-column>
      <el-table-column prop="age" label="年龄" align="center"></el-table-column>
      <el-table-column prop="identity" label="身份证号" align="center"></el-table-column>
      <el-table-column prop="phone" label="手机号" align="center"></el-table-column>
      <el-table-column prop="status" label="就诊状态" align="center" :formatter="UserStatusFormatter"></el-table-column>
      <el-table-column prop="doctorName" label="就诊医师" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="editUser(scope.row.id)">编辑</el-button>
          <el-button link type="primary" size="small" @click="deleteUser(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>

  <!--添加用户弹窗-->
  <el-dialog :title="dialogTitle" v-model="dialogVisible" :before-close="handleClose"
             style="width:1000px;padding:40px;">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="50">
        <el-col :span="12">
          <el-form-item label="用户姓名">
            <el-input placeholder="请输入用户姓名" v-model="saveUserForm.nickname"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="用户密码">
            <el-input placeholder="请设置用户密码" v-model="saveUserForm.password"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="50">
        <el-col :span="12">
          <el-form-item label="用户性别">
            <el-select placeholder="请选择用户性别" v-model="saveUserForm.gender">
              <el-option v-for="item in genderOptions"
                         :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="用户年龄">
            <el-input placeholder="请输入用户年龄" v-model="saveUserForm.age"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="50">
        <el-col :span="12">
          <el-form-item label="用户身份证号">
            <el-input placeholder="请输入用户身份证号" v-model="saveUserForm.identity"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="手机号码">
            <el-input placeholder="请输入用户手机号码" v-model="saveUserForm.phone"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="50">
        <el-col :span="12">
          <el-form-item label="就诊医师">
            <el-input placeholder="请输入就诊医师" v-model="saveUserForm.doctorName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="用户状态">
            <el-select placeholder="请选择用户就诊状态" v-model="saveUserForm.status">
              <el-option v-for="item in StatuOptions"
                         :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button @click="saveUser" type="primary">保存</el-button>
    </template>
  </el-dialog>

</template>
<script setup>
import {onMounted, ref} from "vue";
import qs from "qs";
import {ElMessage} from "element-plus";

//一进页面加载
onMounted(() => {
  loadUser();
});
//定义变量控制弹窗是否显示
const dialogVisible = ref(false);

//定义变量控制弹窗标题
const dialogTitle = ref('添加用户');

//实现关闭功能
const handleClose = () => {
  if (confirm('是否关闭本窗口?')) {
    //用户确认关闭后,将弹窗关闭且将表单数据清空
    dialogVisible.value = false;
    //saveDoctorForm.value = {};
  }
};

//定义数组用来装用户列表数据
const UserArr = ref([]);

//定义数组维护用户性别
const genderOptions = ref([
  {label: '男', value: '1'},
  {label: '女', value: '0'}
]);

//用户性别显示转换
const UserGenderFormatter = (row, column, cellValue, index) => {
  if (!genderOptions.value) {
    return cellValue;
  }
  for (let item of genderOptions.value) {
    if (item.value == cellValue) {
      cellValue = item.label;
    }
  }
  return cellValue;
};

//用户就诊状态显示转换
const UserStatusFormatter = (row, column, cellValue, index) => {
  if (!StatuOptions.value) {
    return cellValue;
  }
  for (let item of StatuOptions.value) {
    if (item.value == cellValue) {
      cellValue = item.label;
    }
  }
  return cellValue;
};

//定义对象保存查询条件
const searchUserForm = ref({
  status: '10'
});

//加载用户表单
const loadUser = () => {
  //有条件则根据条件查用户数据,没有条件默认查所有用户数据
  let data = qs.stringify(searchUserForm.value);
  axios.get(BASE_URL + '/v1/admin/user/select?' + data).then((response) => {
    if (response.data.code == 2000) {
      console.log(response.data.data);
      UserArr.value = response.data.data;
    } else {
      ElMessage.error(response.data.msg);
    }
  })
};

//定义数组维护用户状态
const StatuOptions = ref([
  {label: '未就诊', value: '10'},
  {label: '已就诊', value: '20'}
]);

//定义数组维护用户信息
const saveUserForm = ref({
  id: '',
  username: '',
  nickname: '',
  password: '',
  gender: '',
  age: '',
  identity: '',
  phone: '',
  doctorName: '',
  status: ''
});

//编辑用户
const editUser = (id) => {
  dialogVisible.value = true;//显示弹窗
  dialogTitle.value = '编辑用户';//修改弹窗标题
  //请求当前要编辑的用户信息
  axios.get(BASE_URL + '/v1/user/selectById?id=' + id).then((response) => {
    if (response.data.code == 2000) {
      //将请求到的车辆数据回显在弹窗表单中
      saveUserForm.value = response.data.data;
    } else {
      ElMessage.error(response.data.msg);
    }
  })
};

//添加用户
const saveUser = () => {
  let data = qs.stringify(saveUserForm.value);
  console.log(data);
  axios.post(BASE_URL + '/v1/user/save', data).then((response) => {
    if (response.data.code == 2000) {
      ElMessage.success('保存成功!');
      dialogVisible.value = false;//关闭弹窗
      saveUserForm.value = {};//清空表单输入数据,防止新增成功后还是上一个用户的数据
      loadUser();
    } else {
      ElMessage.error(response.data.msg);
    }
  })
};

//删除用户方法
const deleteUser = (userId) => {
  if (confirm('您确认要删除该用户吗?')) {
    axios.post(BASE_URL + '/v1/user/delete/' + userId).then((response) => {
      if (response.data.code == 2000) {
        ElMessage.success('删除成功!');
        loadUser();
      } else {
        ElMessage.error(response.data.msg);
      }
    })
  }
};

</script>
<style scoped>

</style>